<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Relative Error and Testing</title>
<link rel="stylesheet" href="../../math.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Math Toolkit 4.2.1">
<link rel="up" href="../internals.html" title="Internal tools">
<link rel="prev" href="minimax.html" title="Minimax Approximations and the Remez Algorithm">
<link rel="next" href="test_data.html" title="Graphing, Profiling, and Generating Test Data for Special Functions">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="minimax.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../internals.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="test_data.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="math_toolkit.internals.error_test"></a><a class="link" href="error_test.html" title="Relative Error and Testing">Relative Error and
      Testing</a>
</h3></div></div></div>
<h5>
<a name="math_toolkit.internals.error_test.h0"></a>
        <span class="phrase"><a name="math_toolkit.internals.error_test.synopsis"></a></span><a class="link" href="error_test.html#math_toolkit.internals.error_test.synopsis">Synopsis</a>
      </h5>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">tools</span><span class="special">/</span><span class="identifier">test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
</pre>
<div class="important"><table border="0" summary="Important">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../../doc/src/images/important.png"></td>
<th align="left">Important</th>
</tr>
<tr><td align="left" valign="top"><p>
          The header <code class="computeroutput"><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">tools</span><span class="special">/</span><span class="identifier">test</span><span class="special">.</span><span class="identifier">hpp</span></code> is located under <code class="computeroutput"><span class="identifier">libs</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">include_private</span></code>
          and is NOT installed to the usual locations by default; you will need to
          add <code class="computeroutput"><span class="identifier">libs</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">include_private</span></code> to your compiler's include
          path in order to use this header.
        </p></td></tr>
</table></div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="identifier">T</span> <span class="identifier">relative_error</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">a</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">b</span><span class="special">);</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">A</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">F1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">F2</span><span class="special">&gt;</span>
<span class="identifier">test_result</span><span class="special">&lt;</span><span class="identifier">see</span><span class="special">-</span><span class="identifier">below</span><span class="special">&gt;</span> <span class="identifier">test</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">A</span><span class="special">&amp;</span> <span class="identifier">a</span><span class="special">,</span> <span class="identifier">F1</span> <span class="identifier">test_func</span><span class="special">,</span> <span class="identifier">F2</span> <span class="identifier">expect_func</span><span class="special">);</span>
</pre>
<h5>
<a name="math_toolkit.internals.error_test.h1"></a>
        <span class="phrase"><a name="math_toolkit.internals.error_test.description"></a></span><a class="link" href="error_test.html#math_toolkit.internals.error_test.description">Description</a>
      </h5>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="identifier">T</span> <span class="identifier">relative_error</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">a</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">v</span><span class="special">);</span>
</pre>
<p>
        Returns the relative error between <span class="emphasis"><em>a</em></span> and <span class="emphasis"><em>v</em></span>
        using the usual formula:
      </p>
<div class="blockquote"><blockquote class="blockquote"><p>
          <span class="inlinemediaobject"><img src="../../../equations/error1.svg"></span>

        </p></blockquote></div>
<p>
        In addition the value returned is zero if:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            Both <span class="emphasis"><em>a</em></span> and <span class="emphasis"><em>v</em></span> are infinite.
          </li>
<li class="listitem">
            Both <span class="emphasis"><em>a</em></span> and <span class="emphasis"><em>v</em></span> are denormalised
            numbers or zero.
          </li>
</ul></div>
<p>
        Otherwise if only one of <span class="emphasis"><em>a</em></span> and <span class="emphasis"><em>v</em></span>
        is zero then the value returned is 1.
      </p>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">A</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">F1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">F2</span><span class="special">&gt;</span>
<span class="identifier">test_result</span><span class="special">&lt;</span><span class="identifier">see</span><span class="special">-</span><span class="identifier">below</span><span class="special">&gt;</span> <span class="identifier">test</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">A</span><span class="special">&amp;</span> <span class="identifier">a</span><span class="special">,</span> <span class="identifier">F1</span> <span class="identifier">test_func</span><span class="special">,</span> <span class="identifier">F2</span> <span class="identifier">expect_func</span><span class="special">);</span>
</pre>
<p>
        This function is used for testing a function against tabulated test data.
      </p>
<p>
        The return type contains statistical data on the relative errors (max, mean,
        variance, and the number of test cases etc), as well as the row of test data
        that caused the largest relative error. Public members of type test_result
        are:
      </p>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term"><code class="computeroutput"><span class="keyword">unsigned</span> <span class="identifier">worst</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span></code></span></dt>
<dd><p>
              Returns the row at which the worst error occurred.
            </p></dd>
<dt><span class="term"><code class="computeroutput"><span class="identifier">T</span> <span class="identifier">min</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span></code></span></dt>
<dd><p>
              Returns the smallest relative error found.
            </p></dd>
<dt><span class="term"><code class="computeroutput"><span class="identifier">T</span> <span class="identifier">max</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span></code></span></dt>
<dd><p>
              Returns the largest relative error found.
            </p></dd>
<dt><span class="term"><code class="computeroutput"><span class="identifier">T</span> <span class="identifier">mean</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span></code></span></dt>
<dd><p>
              Returns the mean error found.
            </p></dd>
<dt><span class="term"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">uintmax_t</span> <span class="identifier">count</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span></code></span></dt>
<dd><p>
              Returns the number of test cases.
            </p></dd>
<dt><span class="term"><code class="computeroutput"><span class="identifier">T</span> <span class="identifier">variance</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span></code></span></dt>
<dd><p>
              Returns the variance of the errors found.
            </p></dd>
<dt><span class="term"><code class="computeroutput"><span class="identifier">T</span> <span class="identifier">variance1</span><span class="special">()</span><span class="keyword">const</span><span class="special">;</span></code></span></dt>
<dd><p>
              Returns the unbiased variance of the errors found.
            </p></dd>
<dt><span class="term"><code class="computeroutput"><span class="identifier">T</span> <span class="identifier">rms</span><span class="special">()</span><span class="keyword">const</span></code></span></dt>
<dd><p>
              Returns the Root Mean Square, or quadratic mean of the errors.
            </p></dd>
<dt><span class="term"><code class="computeroutput"><span class="identifier">test_result</span><span class="special">&amp;</span>
          <span class="keyword">operator</span><span class="special">+=(</span><span class="keyword">const</span> <span class="identifier">test_result</span><span class="special">&amp;</span> <span class="identifier">t</span><span class="special">)</span></code></span></dt>
<dd><p>
              Combines two test_result's into a single result.
            </p></dd>
</dl>
</div>
<p>
        The template parameter of test_result, is the same type as the values in
        the two dimensional array passed to function <span class="emphasis"><em>test</em></span>, roughly
        that's <code class="computeroutput"><span class="identifier">A</span><span class="special">::</span><span class="identifier">value_type</span><span class="special">::</span><span class="identifier">value_type</span></code>.
      </p>
<p>
        Parameter <span class="emphasis"><em>a</em></span> is a matrix of test data: and must be a
        standard library Sequence type, that contains another Sequence type: typically
        it will be a two dimensional instance of <code class="literal">std::array</code>. Each
        row of <span class="emphasis"><em>a</em></span> should contain all the parameters that are
        passed to the function under test as well as the expected result.
      </p>
<p>
        Parameter <span class="emphasis"><em>test_func</em></span> is the function under test, it is
        invoked with each row of test data in <span class="emphasis"><em>a</em></span>. Typically type
        F1 is created with Boost.Lambda: see the example below.
      </p>
<p>
        Parameter <span class="emphasis"><em>expect_func</em></span> is a functor that extracts the
        expected result from a row of test data in <span class="emphasis"><em>a</em></span>. Typically
        type F2 is created with Boost.Lambda: see the example below.
      </p>
<p>
        If the function under test returns a non-finite value when a finite result
        is expected, or if a gross error is found, then a message is sent to <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">cerr</span></code>,
        and a call to BOOST_ERROR() made (which means that including this header
        requires you use Boost.Test). This is mainly a debugging/development aid
        (and a good place for a breakpoint).
      </p>
<h5>
<a name="math_toolkit.internals.error_test.h2"></a>
        <span class="phrase"><a name="math_toolkit.internals.error_test.example"></a></span><a class="link" href="error_test.html#math_toolkit.internals.error_test.example">Example</a>
      </h5>
<p>
        Suppose we want to test the <code class="computeroutput"><span class="identifier">tgamma</span></code>
        and <code class="computeroutput"><span class="identifier">lgamma</span></code> functions, we
        can create a two-dimensional matrix of test data, each row is one test case,
        and contains three elements: the input value, and the expected results for
        the <code class="computeroutput"><span class="identifier">tgamma</span></code> and <code class="computeroutput"><span class="identifier">lgamma</span></code> functions respectively.
      </p>
<pre class="programlisting"><span class="keyword">static</span> <span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">array</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">array</span><span class="special">&lt;</span><span class="identifier">TestType</span><span class="special">,</span> <span class="number">3</span><span class="special">&gt;,</span> <span class="identifier">NumberOfTests</span><span class="special">&gt;</span>
   <span class="identifier">factorials</span> <span class="special">=</span> <span class="special">{</span>
      <span class="comment">/* big array of test data goes here */</span>
   <span class="special">};</span>
</pre>
<p>
        Now we can invoke the test function to test tgamma:
      </p>
<pre class="programlisting"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">tools</span><span class="special">;</span>
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">lambda</span><span class="special">;</span>

<span class="comment">// get a pointer to the function under test:</span>
<span class="identifier">TestType</span> <span class="special">(*</span><span class="identifier">funcp</span><span class="special">)(</span><span class="identifier">TestType</span><span class="special">)</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">tgamma</span><span class="special">;</span>

<span class="comment">// declare something to hold the result:</span>
<span class="identifier">test_result</span><span class="special">&lt;</span><span class="identifier">TestType</span><span class="special">&gt;</span> <span class="identifier">result</span><span class="special">;</span>
<span class="comment">//</span>
<span class="comment">// and test tgamma against data:</span>
<span class="comment">//</span>
<span class="identifier">result</span> <span class="special">=</span> <span class="identifier">test</span><span class="special">(</span>
   <span class="identifier">factorials</span><span class="special">,</span>
   <span class="identifier">bind</span><span class="special">(</span><span class="identifier">funcp</span><span class="special">,</span> <span class="identifier">ret</span><span class="special">&lt;</span><span class="identifier">TestType</span><span class="special">&gt;(</span><span class="identifier">_1</span><span class="special">[</span><span class="number">0</span><span class="special">])),</span> <span class="comment">// calls tgamma with factorials[row][0]</span>
   <span class="identifier">ret</span><span class="special">&lt;</span><span class="identifier">TestType</span><span class="special">&gt;(</span><span class="identifier">_1</span><span class="special">[</span><span class="number">1</span><span class="special">])</span>               <span class="comment">// extracts the expected result from factorials[row][1]</span>
<span class="special">);</span>
<span class="comment">//</span>
<span class="comment">// Print out some results:</span>
<span class="comment">//</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"The Mean was "</span> <span class="special">&lt;&lt;</span> <span class="identifier">result</span><span class="special">.</span><span class="identifier">mean</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"The worst error was "</span> <span class="special">&lt;&lt;</span> <span class="special">(</span><span class="identifier">result</span><span class="special">.</span><span class="identifier">max</span><span class="special">)()</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"The worst error was at row "</span> <span class="special">&lt;&lt;</span> <span class="identifier">result</span><span class="special">.</span><span class="identifier">worst_case</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="comment">//</span>
<span class="comment">// same again with lgamma this time:</span>
<span class="comment">//</span>
<span class="identifier">funcp</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">lgamma</span><span class="special">;</span>
<span class="identifier">result</span> <span class="special">=</span> <span class="identifier">test</span><span class="special">(</span>
   <span class="identifier">factorials</span><span class="special">,</span>
   <span class="identifier">bind</span><span class="special">(</span><span class="identifier">funcp</span><span class="special">,</span> <span class="identifier">ret</span><span class="special">&lt;</span><span class="identifier">TestType</span><span class="special">&gt;(</span><span class="identifier">_1</span><span class="special">[</span><span class="number">0</span><span class="special">])),</span> <span class="comment">// calls tgamma with factorials[row][0]</span>
   <span class="identifier">ret</span><span class="special">&lt;</span><span class="identifier">TestType</span><span class="special">&gt;(</span><span class="identifier">_1</span><span class="special">[</span><span class="number">2</span><span class="special">])</span>               <span class="comment">// extracts the expected result from factorials[row][2]</span>
<span class="special">);</span>
<span class="comment">//</span>
<span class="comment">// etc ...</span>
<span class="comment">//</span>
</pre>
</div>
<div class="copyright-footer">Copyright © 2006-2021 Nikhar Agrawal, Anton Bikineev, Matthew Borland,
      Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert Holin, Bruno
      Lalande, John Maddock, Evan Miller, Jeremy Murphy, Matthew Pulver, Johan Råde,
      Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, Daryle
      Walker and Xiaogang Zhang<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="minimax.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../internals.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="test_data.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
